<template>
	<Dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
		<ContentWrap>
			<!-- 搜索工作栏 -->
			<el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
				<el-form-item label="" prop="keyWord">
					<el-input placeholder="请输入楼栋名称" v-model.trim="queryParams.keyWord" />
				</el-form-item>
				<el-form-item>
					<el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
				</el-form-item>
			</el-form>
		</ContentWrap>

		<!-- 列表 -->
		<ContentWrap>
			<el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
				<el-table-column label="ID" align="center" prop="id" width="100" />
				<el-table-column label="社区名称" align="left" prop="community.name" width="160" />
				<el-table-column label="房屋地址" align="left" prop="name" />
				<el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="120">
					<template #default="scope">
						<el-button type="primary" link @click="confirmMemberUser(scope.row)">选择</el-button>
					</template>
				</el-table-column>
			</el-table>
			<Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList" />
		</ContentWrap>
	</Dialog>
</template>
<script lang="ts" setup>
	import * as HouseRoomApi from '@/api/zwsj/houseRoom'

	defineOptions({ name: 'BuildingAddressSelect' })

	const emits = defineEmits(['confirmBuildingAddress'])

	const message = useMessage() // 消息弹窗
	const { t } = useI18n() // 国际化

	const dialogVisible = ref(false) // 弹窗的是否展示
	const dialogTitle = ref('') // 弹窗的标题
	const loading = ref(true) // 列表的加载中
	const total = ref(0) // 列表的总页数
	const list = ref() // 列表的数据

	const queryParams = reactive({
		pageNo: 1,
		pageSize: 10,
		parentId: 0,
		keyWord: undefined,
		commId: undefined,
	})

	/** 打开弹窗 */
	const open = async (commId?: number) => {
		if (commId) {
			queryParams.commId = commId
		}
		dialogVisible.value = true
		dialogTitle.value = '选择楼栋地址'
		getList()
	}
	defineExpose({ open }) // 提供 open 方法，用于打开弹窗

	const queryFormRef = ref() // 搜索的表单

	/** 查询部门列表 */
	const getList = async () => {
		loading.value = true
		try {
			const data = await HouseRoomApi.getHouseRoomPage(queryParams)
			list.value = data.list
			total.value = data.total
		} finally {
			loading.value = false
		}
	}

	/** 搜索按钮操作 */
	const handleQuery = () => {
		getList()
	}

	/** 确认 */
	const confirmMemberUser = async (row: any) => {
		emits('confirmBuildingAddress', row)
		dialogVisible.value = false
	}

	/** 初始化 **/
	onMounted(async () => {
		await getList()
	})
</script>
